package cn.com.nou.base.controller.bdm;

import cn.com.nou.base.service.bdm.LearningFormService;
import cn.com.nou.common.BaseResponse;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.Pagination;
import io.swagger.annotations.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;

@RestController
@RequestMapping(value = "/learningform",produces="application/json;charset=UTF-8")
@Api(description = "学习形式管理")
public class LearningFormController {

    @Autowired
    private LearningFormService learningFormService;

    private final Log log = LogFactory.getLog(getClass());

    /**
     * 取得学习形式列表接口
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    @ApiOperation(value="取得学习形式列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = true, dataType = "string"),
            @ApiImplicitParam(name = "type", value = "类型 learningForm", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "学习形式列表获取失败"),
            @ApiResponse(code = 200, message = "学习形式列表获取成功")
    })
    @GetMapping("/list")
    public ResponseEntity<Pagination> getLearningForm(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            Pagination pagination = learningFormService.getEnumByType(nouEnumerationT);
            return BaseResponse.generateOKResponseEntity(pagination);
        } catch (Exception e) {
            log.debug("LearningFormController-->>getLearningForm",e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", e.getMessage());
        }
    }

    /**
     * 添加学习形式
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    @ApiOperation(value="添加学习形式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型 learningForm", required = true, dataType = "string"),
            @ApiImplicitParam(name = "itemCode", value = "学习形式代码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "itemName", value = "学习形式名称", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "学习形式新增失败"),
            @ApiResponse(code = 200, message = "学习形式新增成功")
    })
    @PostMapping("/add")
    public ResponseEntity<BaseResponse> addLearningForm(@RequestBody NouEnumerationT nouEnumerationT) throws Exception {
        try {
            int i = learningFormService.addEnumByType(nouEnumerationT);
            if (i == 0) {
                return BaseResponse.generateBadResponseEntity(500, "新增失败，学习形式代码或名称重复请重新提交", "");
            } else {
                return BaseResponse.generateOKResponseEntity("学习形式新增成功", "");
            }
        } catch (Exception e) {
            log.debug("LearningFormController-->>addLearningForm",e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", e.getMessage());
        }
    }

    /**
     * 编辑学习形式
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    @ApiOperation(value="编辑学习形式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学习形式id", required = true, dataType = "string"),
            @ApiImplicitParam(name = "type", value = "类型 learningForm", required = true, dataType = "string"),
            @ApiImplicitParam(name = "itemCode", value = "学习形式代码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "itemName", value = "学习形式名称", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "编辑学习形式失败"),
            @ApiResponse(code = 200, message = "编辑学习形式成功")
    })
    @PutMapping("/update")
    public ResponseEntity<Response> updateLearningForm(@RequestBody NouEnumerationT nouEnumerationT) throws Exception {
        try {
            int i = learningFormService.updateEnumById(nouEnumerationT);
            if (i == 0){
                return BaseResponse.generateBadResponseEntity(500, "修改失败，学习形式代码或名称重复请重新提交", "");
            } else {
                return BaseResponse.generateOKResponseEntity("学习形式修改成功", "");
            }
        } catch (Exception e) {
            log.debug("LearningFormController-->>addLearningForm",e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", e.getMessage());
        }
    }

    /**
     * 删除学习形式
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    @ApiOperation(value="删除学习形式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学习形式id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "用户删除失败"),
            @ApiResponse(code = 200, message = "用户删除成功")
    })
    @DeleteMapping("/delete")
    public ResponseEntity<Response> deleteLearningForm(@RequestBody NouEnumerationT nouEnumerationT) throws Exception {
        try {
            int i = learningFormService.deleteEnumById(nouEnumerationT);
            if (i == 0){
                return BaseResponse.generateBadResponseEntity(500, "删除学习形式失败，请确认该id是否存在", "");
            } else {
                return BaseResponse.generateOKResponseEntity("删除学习形式成功", "");
            }
        } catch (Exception e) {
            log.debug("LearningFormController-->>addLearningForm",e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", e.getMessage());
        }
    }

}
